package com.atguigu.tingshu.user.pattren.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattren.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: jack
 * @date: 2025/10/14 19:16
 */

@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
@Slf4j
public class VipDelivery implements DeliveryStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     *
     * @param userPaidRecordVo
     */
    @Override
    public void delivery(UserPaidRecordVo userPaidRecordVo) {

        log.info("开始处理vip购买");
        //3. 判断当前订单是否被处理
        Long count = userVipServiceMapper.selectCount(
                new LambdaQueryWrapper<UserVipService>()
                        .eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo())
        );
        if (count > 0) {
            return;
        }
        //3.1 为用户发放权益
        //3.1.1 查询当前用户是否为vip
        Boolean isVip = false;
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            isVip = true;
        }


        UserVipService userVipService = new UserVipService();
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Date now = new Date();
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        if (!isVip) {
            //3.2 封装购买记录
            //3.2.1 封装购买记录
            //3.2.2 封装用户信息
            userVipService.setStartTime(now);
            DateTime dateTime = DateUtil.offsetMonth(now, serviceMonth);
            userVipService.setExpireTime(dateTime);
            //3.3 设置过期时间
        } else {
            userVipService.setStartTime(userInfo.getVipExpireTime());
            userVipService.setExpireTime(DateUtil.offsetMonth(userInfo.getVipExpireTime(), serviceMonth));
        }
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipServiceMapper.insert(userVipService);
        //3.4发放权益
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        UserInfo userInfo1 = BeanUtil.copyProperties(userInfo, UserInfo.class);
        userInfoMapper.updateById(userInfo1);
    }
}
